package server;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * User: tum0r
 * Package Name: server
 * Time: 2019-03-10 Sunday 01:38:21
 */
public class SettingServer extends BaseServer {
    public JSONObject readSettingObject(String settingName) {
        JSONObject result = JSONObject.fromObject("{}");
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select * from setting where name=?", new Object[]{settingName});
        if (temp.size() != 0) {
            result = JSONObject.fromObject(temp.getFirst().get("content"));
        }
        return result;
    }

    public JSONArray readSettingArray(String settingName) {
        JSONArray result = JSONArray.fromObject("[]");
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select * from setting where name=?", new Object[]{settingName});
        if (temp.size() != 0) {
            result = JSONArray.fromObject(temp.getFirst().get("content"));
        }
        return result;
    }

    public boolean recordSettingObject(String settingName, String content) {
        return recordSettingObject(settingName, JSONObject.fromObject(content));
    }

    public boolean recordSettingObject(String settingName, JSONObject contentObject) {
        return sql.update("update setting set content=? where name=?", new Object[]{contentObject.toString(), settingName}) == 1;
    }

    public boolean recordSettingObject(String settingName, JSONArray contentArray) {
        return sql.update("update setting set content=? where name=?", new Object[]{contentArray.toString(), settingName}) == 1;
    }
}
